package com.yashi.springmvcjbpm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yashi.springmvcjbpm.dao.impl.LeaveDAOImpl;
import com.yashi.springmvcjbpm.dao.impl.StaffDAOImpl;
import com.yashi.springmvcjbpm.service.JBPMService;
import com.yashi.springmvcjbpm.service.impl.JBPMServiceImpl;
import com.yashi.springmvcjbpm.util.JBPMUtil;

@Controller
public class TaskListController {
	private List taskList = new ArrayList();
	private JBPMServiceImpl jbpmServiceImpl = new JBPMServiceImpl();
	private StaffDAOImpl staffDaoImpl = new StaffDAOImpl();
	private StaffDAOImpl daoImpl = new StaffDAOImpl();
	private LeaveDAOImpl leaveDAOImpl = new LeaveDAOImpl();
	@RequestMapping("/taskListAction")
	public String getTasks(String assignee, HttpServletRequest request) {

		System.out.println("assignee=" + assignee);
		String staffName = (String) request.getSession().getAttribute("staffName");
		System.out.println("staffName=" + staffName);
		String staffPosition = staffDaoImpl.getStaffPosition(staffName);
		System.out.println("staffPosition=" + staffPosition);
		if (staffPosition.equals(assignee)) {
			taskList = jbpmServiceImpl.getTasksList(staffName);
			request.setAttribute("taskList", taskList);
		} else {
			return "error";
		}		
		return "taskList"; // 查看请假列表
	}

	@RequestMapping("/taskquery")
	public String taskquery(String assignee, HttpServletRequest request) {
		String staffName = (String) request.getSession().getAttribute("staffName");
		String staffPosition = staffDaoImpl.getStaffPosition(staffName);
		
		System.out.println("staffPosition="+staffPosition+",assignee="+assignee);
		if (staffPosition.equals(assignee)) {
			String sql = null;
			if (staffPosition.equals(assignee)&&assignee.equals("boss")) {
				 sql = "select * from `leave` left join jbpm4_hist_procinst p on p.ID_=`leave`.leaveInstanceId";							
			}else {
				sql = "select * from `leave` left join jbpm4_hist_procinst p on p.ID_=`leave`.leaveInstanceId where staffName='" + staffName + "'";
			}
			taskList = leaveDAOImpl.findLeaves(sql);
			request.setAttribute("taskList", taskList);
			request.setAttribute("assignee", assignee);
		}else {
			return "error";
		}
		return "taskQueryList";

	}

	@RequestMapping("taskListActionApply")
	public String apply(String assignee, HttpServletRequest request) {
		String staffName = (String) request.getSession().getAttribute("staffName");
		String staffPosition = staffDaoImpl.getStaffPosition(staffName);
		String go = null;
		if (staffPosition.equals(assignee)) { // 用户身份
			taskList = jbpmServiceImpl.getTasksList(staffName);			
			if (taskList.size() == 0) { // 如果没有任务,跳转到请求页面
				go= "request";
			} else { // 如果有被驳回的任务，跳转到原请求列表页面
				List<Task> list = new ArrayList<>();
				for (int j = 0; j < taskList.size(); j++) {
					Task task = (Task)taskList.get(j);
					if(task.getName().equals("申请")){
						list.add(task);
					}
					
				}
				if(list.size()>0){
					request.setAttribute("taskList", list);
					go= "requestList";	
				}else{
					go= "request";
				}
			}
		}else {
			return "error";
		}
		return go;

	}
	@RequestMapping(value="/deleteAction")
	public String delete(String proId,HttpServletRequest request,String assignee,String state) {
		System.out.println("assignee===="+state);
		if(state.equals("ended")) {
			request.getSession().setAttribute("mes", "已请假不允许撤销！");
		}else {
		JBPMUtil jbpmUtil = new JBPMUtil();
		jbpmUtil.deleteProcessInstance(proId);
		leaveDAOImpl.deleteLeave(proId);
		}
		return taskquery(assignee, request);
	}
}
